package com.sansecy.mocklocationtest.loadedapk;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sansecy.mocklocationtest.AppInfo;
import com.sansecy.mocklocationtest.R;
import com.sansecy.mocklocationtest.utils.ApkFileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class LaodedApkFragment extends Fragment {

    public LaodedApkFragment() {
    }


    public static LaodedApkFragment newInstance() {
        LaodedApkFragment fragment = new LaodedApkFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_laoded_apk, container, false);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(requireActivity(), 4));
        loadedAppListAdapter adapter = new loadedAppListAdapter(requireActivity());
        recyclerView.setAdapter(adapter);
        File appFolder = ApkFileUtils.getAppFolder();
        File[] files = appFolder.listFiles();
        List<File> fileList = Arrays.asList(files);
        ArrayList<AppInfo> appInfos = new ArrayList<>();
        for (File file : fileList) {
            AppInfo appInfo = new AppInfo(file.getName(), file.getAbsolutePath());
            appInfos.add(appInfo);
        }
        adapter.setData(appInfos);
        return view;
    }
}